package com.ydd.controller.admin;

import com.ydd.constant.JwtClaimsConstant;
import com.ydd.dto.SysUserLoginDTO;
import com.ydd.entity.SysUser;
import com.ydd.properties.JwtProperties;
import com.ydd.result.Result;
import com.ydd.service.IConfigService;
import com.ydd.service.ISysUserService;
import com.ydd.utils.JwtUtil;
import com.ydd.vo.BaseInfoVO;
import com.ydd.vo.ConfigVO;
import com.ydd.vo.UserProfileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author itbobo
 * @Date 2023-05-30 21:19
 */
@Api(tags = "基础模块相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/index")
public class IndexController {

    @Autowired
    private IConfigService configService;

    @Autowired
    private ISysUserService userService;

    /**
     * 获取系统配置接口
     *
     * @return
     */
    @ApiOperation("获取系统配置接口")
    @GetMapping("/getConfig")
    public Result<ConfigVO> getConfig() {
        //打印日志信息
        log.info("获取系统配置...");
        //调用业务层方法
        ConfigVO configVO = configService.getConfig();
        //返回数据
        return Result.success(configVO);
    }

    /**
     * 主页统计信息接口
     *
     * @return
     */
    @ApiOperation("主页统计信息接口")
    @GetMapping("/baseInfo")
    public Result<BaseInfoVO> baseInfo() {
        //打印日志信息
        log.info("主页统计信息..");
        //调用业务层方法
        BaseInfoVO baseInfovo = configService.baseInfo();
        //返回数据
        return Result.success(baseInfovo);
    }

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录接口
     *
     * @param userLoginDTO
     * @return
     */
    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody SysUserLoginDTO userLoginDTO) {
        //打印日志信息
        log.info("登录:{}", userLoginDTO);
        SysUser user = userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.SYSUSER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        //返回token
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        return Result.success(map);
    }

    /**
     * 获取用户资料接口
     * @return
     */
    @ApiOperation("获取用户资料接口")
    @PostMapping("/profile")
    public Result<UserProfileVO> profile(){
        //打印日志信息
        log.info("获取用户资料....");
        //调用业务层方法
        UserProfileVO userProfileVO = configService.getUserProfile();
        //返回信息
        return Result.success(userProfileVO);
    }
}
